﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchoolWeb.Models;
using System.Web.Security;

namespace GameSchoolWeb.Controllers
{
	[Authorize]
	public class HomeController : Controller
	{
		ICourseRepository m_courseRepository;
		IUserRepository m_userRepository;


		public HomeController()
		{
			m_courseRepository = new CourseRepository();
			m_userRepository = new UserRepository();
		}

		public HomeController(ICourseRepository courseRep)
		{
			m_courseRepository = courseRep;
		}

		public HomeController(IUserRepository userRep)
		{
			m_userRepository = userRep;
		}


		public ActionResult Index()
		{
			// UserGuid er auðkenni núverandi notanda
			Guid UserGuid = (Guid)Membership.GetUser().ProviderUserKey;

			// allNews eru allar fréttir sem núverandi notandi hefur aðgang að.
			var allNews = from n in m_courseRepository.NewsItems()
						  join m in m_courseRepository.UserRegistrations() on n.courseId equals m.courseId
						  where m.UserId == UserGuid
						  orderby n.dDateCreated ascending
						  select n;

			return View(allNews);
		}

		public ActionResult About()
		{
			return View();
		}
	}
}
